package hw.ats.news.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.tattermedia.allthataudio.misc.DebugFlag;

/**
 * 글
 * @author pcraft
 */
public class News {
	private static final String TAG = "News";
	
	public int uid;
	public String url = "";
	public String title = "";
	public String thumb_default = "";
	public String thumb_list = "";
	public String main_image = "";
	public Date regdate = null;
	public String body_html = "";
	public String body_text = "";
	public String movie = "";
	public String author = "";
	public int cate_uid;
	public String cate_name = "";
	public int cmt_cnt = 0;
	public boolean recmd_flag = false;
	public boolean favor_flag = false;
	public boolean photo_flag = false;
	public boolean movie_flag = false;
	
	// audio
	public String 	mp3_url = "";
	public long		mp3_fileSize;
	public long 	mp3_totalDuration;
	
	public News() {
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("uid=").append(uid).append(", ");
		sb.append("url=").append(url).append(", ");
		sb.append("title=").append(title).append(", ");
		sb.append("thumb_default=").append(thumb_default).append(", ");
		sb.append("thumb_list=").append(thumb_list).append(", ");
		sb.append("main_image=").append(main_image).append(", ");
		sb.append("regdate=").append(regdate).append(", ");
		sb.append("body_html=").append(body_html).append(", ");
		sb.append("body_text=").append(body_text).append(", ");
		sb.append("movie=").append(movie).append(", ");
		sb.append("cate_name=").append(cate_name).append(", ");
		sb.append("cate_uid=").append(cate_uid).append(", ");
		sb.append("author=").append(author).append(", ");
		sb.append("cmt_cnt=").append(cmt_cnt).append(", ");
		sb.append("recmd_flag=").append(recmd_flag).append(", ");
		sb.append("favor_flag=").append(favor_flag).append(", ");
		sb.append("photo_flag=").append(photo_flag).append(", ");
		sb.append("movie_flag=").append(movie_flag);
		
		return sb.toString();
	}
	
	private void bind(Cursor c) {
		this.uid = c.getInt(c.getColumnIndex("uid"));
		this.url = c.getString(c.getColumnIndex("url"));
		this.title = c.getString(c.getColumnIndex("title"));
		this.thumb_default = c.getString(c.getColumnIndex("thumb_default"));
		this.thumb_list = c.getString(c.getColumnIndex("thumb_list"));
		this.main_image = c.getString(c.getColumnIndex("main_image"));
		this.regdate = new Date(c.getLong(c.getColumnIndex("regdate")));
		this.body_html = c.getString(c.getColumnIndex("body_html"));
		this.body_text = c.getString(c.getColumnIndex("body_text"));
		this.movie = c.getString(c.getColumnIndex("movie"));
		this.cate_name = c.getString(c.getColumnIndex("cate_name"));
		this.cate_uid = c.getInt(c.getColumnIndex("cate_uid"));
		this.author = c.getString(c.getColumnIndex("author"));
		this.cmt_cnt = c.getInt(c.getColumnIndex("cmt_cnt"));
		this.recmd_flag = c.getInt(c.getColumnIndex("recmd_flag")) > 0;
		this.favor_flag = c.getInt(c.getColumnIndex("favor_flag")) > 0;
		this.photo_flag = c.getInt(c.getColumnIndex("photo_flag")) > 0;
		this.movie_flag = c.getInt(c.getColumnIndex("movie_flag")) > 0;
		
		// audio
		this.mp3_url = c.getString(c.getColumnIndex("mp3_url"));
		this.mp3_fileSize = c.getLong(c.getColumnIndex("mp3_fileSize"));
		this.mp3_totalDuration = c.getLong(c.getColumnIndex("mp3_totalDuration"));
	}
	
	public void insert(SQLiteDatabase db) {
		try {
			ContentValues v = new ContentValues();
			v.put("uid", uid);
			v.put("url", url);
			v.put("title", title);
			v.put("thumb_default", thumb_default);
			v.put("thumb_list", thumb_list);
			v.put("main_image", main_image);
			if (regdate == null)
				regdate = new Date();
			v.put("regdate", regdate.getTime());
			v.put("body_html", body_html);
			v.put("body_text", body_text);
			v.put("movie", movie);
			v.put("cate_name", cate_name);
			v.put("cate_uid", cate_uid);
			v.put("author", author);
			v.put("cmt_cnt", cmt_cnt);
			v.put("recmd_flag", recmd_flag ? 1 : 0);
			v.put("favor_flag", favor_flag ? 1 : 0);
			v.put("photo_flag", photo_flag ? 1 : 0);
			v.put("movie_flag", movie_flag ? 1 : 0);
			
			// audio
			v.put("mp3_url", mp3_url);
			v.put("mp3_fileSize", mp3_fileSize);
			v.put("mp3_totalDuration", mp3_totalDuration);
			
			db.insert("news", null, v);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}
	
	public void updateBody(SQLiteDatabase db) {
		try {
			ContentValues v = new ContentValues();
			v.put("body_html", body_html);
			
			// audio
			v.put("mp3_url", mp3_url);
			v.put("mp3_fileSize", mp3_fileSize);
			v.put("mp3_totalDuration", mp3_totalDuration);
			
			db.update("news", v, "uid = " + uid, null);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}
	
	/**
	 * 즐겨찾는 글 처리
	 * @param mark
	 */
	public void markFavorite(SQLiteDatabase db, boolean mark) {
		try {
			ContentValues v = new ContentValues();
			v.put("favor_flag", mark ? 1 : 0);
			db.update("news", v, "uid = " + uid, null);
			favor_flag = mark;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}
	
	/**
	 * 추천 글 처리
	 * @param mark
	 */
	public static void markRecmd(SQLiteDatabase db, int uid, boolean mark) {
		try {
			ContentValues v = new ContentValues();
			v.put("recmd_flag", mark ? 1 : 0);
			db.update("news", v, "uid = " + uid, null);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}
	
	/**
	 * 글 1개 로드
	 * @param uid
	 * @return
	 */
	public static News load(SQLiteDatabase db, int uid) {
		News entry = null;
		Cursor c = null;
		try {
			c = db.query("news", null, "uid = " + uid, null, null, null, null);
			if (!c.moveToFirst()) {
				return entry;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	/**
	 * 글 존재 여부 확인
	 * @param uid
	 * @return
	 */
	public static boolean exists(SQLiteDatabase db, int uid) {
		Cursor c = null;
		try {
			c = db.rawQuery("SELECT uid FROM news WHERE uid="+uid+" LIMIT 1", null);
			if (!c.moveToFirst()) {
				return false;
			}
			
			return c.getInt(0) > 0;
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
	}
	
	/**
	 * 글 목록 (전체)
	 * @param start 시작 레코드 번호
	 * @param rows 패치할 레코드 개수
	 * @return
	 */
	public static List<News> list(SQLiteDatabase db, int start, int rows) {
		List<News> list = new ArrayList<News>();
		Cursor c = null;
		try {
			c = db.rawQuery("SELECT * FROM news ORDER BY uid DESC LIMIT "+rows+" OFFSET "+start, null);
			if (!c.moveToFirst()) {
				return list;
			}

			do {
				News entry = new News();
				entry.bind(c);
				list.add(entry);
			} while (c.moveToNext());
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return list;
	}
	
	public static News prev(SQLiteDatabase db, int uid, int cid) {
		String sql = "SELECT * "
			       + "FROM   news "
			       + "WHERE  uid > "+uid+" ";
		
		if (cid > 0)
			sql += "AND cate_uid="+cid+" "; 
		
		sql 	  += "ORDER  "
			       + "BY     uid ASC "
			       + "LIMIT  1";
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	public static News next(SQLiteDatabase db, int uid, int cid) {
		String sql = "SELECT * "
		       + "FROM   news "
		       + "WHERE  uid < "+uid+" ";
		
		if (cid > 0)
			sql += "AND cate_uid="+cid+" "; 
		
		sql   += "ORDER  "
		       + "BY     uid DESC "
		       + "LIMIT  1";
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	/**
	 * 추천 글 목록 (전체)
	 * @param start 시작 레코드 번호
	 * @param rows 패치할 레코드 개수
	 * @return
	 */
	public static List<News> listRecmd(SQLiteDatabase db, int start, int rows) {
		List<News> list = new ArrayList<News>();
		Cursor c = null;
		try {
			c = db.rawQuery("SELECT * FROM news WHERE recmd_flag=1 ORDER BY uid DESC LIMIT "+rows+" OFFSET "+start, null);
			if (!c.moveToFirst()) {
				return list;
			}

			do {
				News entry = new News();
				entry.bind(c);
				list.add(entry);
			} while (c.moveToNext());
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return list;
	}
	
	public static News prevRecmd(SQLiteDatabase db, int uid, int cid) {
		String sql = "SELECT * "
			       + "FROM   news "
			       + "WHERE  uid > "+uid+" "
			       + "AND    recmd_flag = 1 ";
		
		if (cid > 0)
			sql += "AND cate_uid="+cid+" "; 
		
		sql       += "ORDER  "
			       + "BY     uid ASC "
			       + "LIMIT  1";
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	public static News nextRecmd(SQLiteDatabase db, int uid, int cid) {
		String sql = "SELECT * "
		       + "FROM   news "
		       + "WHERE  uid < "+uid+" "
		       + "AND    recmd_flag = 1 ";
		
		if (cid > 0)
			sql += "AND cate_uid="+cid+" "; 
		
		 sql  += "ORDER  "
		       + "BY     uid DESC "
		       + "LIMIT  1";
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	/**
	 * 글 목록
	 * @param cate_id
	 * @param start 시작 레코드 번호
	 * @param rows 패치할 레코드 개수
	 * @return
	 */
	public static List<News> list(SQLiteDatabase db, int cate_uid, int start, int rows) {
		List<News> list = new ArrayList<News>();
		Cursor c = null;
		try {
			c = db.rawQuery("SELECT * FROM news WHERE cate_uid="+cate_uid+" ORDER BY uid DESC LIMIT "+rows+" OFFSET "+start, null);
			if (!c.moveToFirst()) {
				return list;
			}
			
			do {
				News entry = new News();
				entry.bind(c);
				list.add(entry);
			} while (c.moveToNext());
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return list;
	}
	
//	public static News prev(SQLiteDatabase db, int uid, int cate_uid) {
//		String sql = "SELECT * "
//			       + "FROM   news "
//			       + "WHERE  uid > "+uid+" "
//			       + "AND    cate_uid = "+cate_uid+" "
//			       + "ORDER  "
//			       + "BY     uid ASC "
//			       + "LIMIT  1";
//		News entry = null;
//		Cursor c = null;
//		try {
//			c = db.rawQuery(sql, null);
//			if (!c.moveToFirst()) {
//				return null;
//			}
//			entry = new News();
//			entry.bind(c);
//		} finally {
//			try { c.close(); } catch (Exception ignored) {}
//		}
//		
//		return entry;
//	}
	
//	public static News next(SQLiteDatabase db, int uid, int cate_uid) {
//		String sql = "SELECT * "
//		       + "FROM   news "
//		       + "WHERE  uid < "+uid+" "
//		       + "AND    cate_uid = "+cate_uid+" "
//		       + "ORDER  "
//		       + "BY     uid DESC "
//		       + "LIMIT  1";
//		News entry = null;
//		Cursor c = null;
//		try {
//			c = db.rawQuery(sql, null);
//			if (!c.moveToFirst()) {
//				return null;
//			}
//			entry = new News();
//			entry.bind(c);
//		} finally {
//			try { c.close(); } catch (Exception ignored) {}
//		}
//		
//		return entry;
//	}
	
	/**
	 * 추천 글 목록
	 * @param cate_id
	 * @param start 시작 레코드 번호
	 * @param rows 패치할 레코드 개수
	 * @return
	 */
	public static List<News> listRecmd(SQLiteDatabase db, int cate_uid, int start, int rows) {
		List<News> list = new ArrayList<News>();
		Cursor c = null;
		try {
			c = db.rawQuery("SELECT * FROM news WHERE cate_uid="+cate_uid+" AND recmd_flag=1 ORDER BY uid DESC LIMIT "+rows+" OFFSET "+start, null);
			if (!c.moveToFirst()) {
				return list;
			}
			
			do {
				News entry = new News();
				entry.bind(c);
				list.add(entry);
			} while (c.moveToNext());
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return list;
	}
	
//	public static News prevRecmd(SQLiteDatabase db, int uid, int cid) {
//		String sql = "SELECT * "
//			       + "FROM   news "
//			       + "WHERE  uid > "+uid+" "
//			       + "AND    recmd_flag = 1 ";
//		
//		if (cid > 0)
//			sql += "AND cate_uid="+cid+" "; 
//		
//		sql	      += "ORDER  "
//			       + "BY     uid ASC "
//			       + "LIMIT  1";
//		News entry = null;
//		Cursor c = null;
//		try {
//			c = db.rawQuery(sql, null);
//			if (!c.moveToFirst()) {
//				return null;
//			}
//			entry = new News();
//			entry.bind(c);
//		} finally {
//			try { c.close(); } catch (Exception ignored) {}
//		}
//		
//		return entry;
//	}
	
//	public static News nextRecmd(SQLiteDatabase db, int uid, int cid) {
//		String sql = "SELECT * "
//		       + "FROM   news "
//		       + "WHERE  uid < "+uid+" "
//		       + "AND    recmd_flag = 1 ";
//		if (cid > 0)
//			sql += "AND cate_uid="+cid+" "; 
//		sql   += "ORDER  "
//		       + "BY     uid DESC "
//		       + "LIMIT  1";
//		News entry = null;
//		Cursor c = null;
//		try {
//			c = db.rawQuery(sql, null);
//			if (!c.moveToFirst()) {
//				return null;
//			}
//			entry = new News();
//			entry.bind(c);
//		} finally {
//			try { c.close(); } catch (Exception ignored) {}
//		}
//		
//		return entry;
//	}
	
	/**
	 * 즐겨찾기 글 목록
	 * @param start 시작 레코드 번호
	 * @param rows 패치할 레코드 개수
	 * @return
	 */
	public static List<News> listFavor(SQLiteDatabase db, int start, int rows) {
		List<News> list = new ArrayList<News>();
		Cursor c = null;
		try {
			c = db.rawQuery("SELECT * FROM news WHERE favor_flag=1 ORDER BY uid DESC LIMIT "+rows+" OFFSET "+start, null);
			if (!c.moveToFirst()) {
				return list;
			}
			
			do {
				News entry = new News();
				entry.bind(c);
				list.add(entry);
			} while (c.moveToNext());
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return list;
	}
	
	public static News prevFavor(SQLiteDatabase db, int uid) {
		String sql = "SELECT * "
			       + "FROM   news "
			       + "WHERE  uid > "+uid+" "
			       + "AND    favor_flag = 1 "
			       + "ORDER  "
			       + "BY     uid ASC "
			       + "LIMIT  1";
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	public static News nextFavor(SQLiteDatabase db, int uid) {
		String sql = "SELECT * "
		       + "FROM   news "
		       + "WHERE  uid < "+uid+" "
		       + "AND    favor_flag = 1 "
		       + "ORDER  "
		       + "BY     uid DESC "
		       + "LIMIT  1";
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	/**
	 * 글 검색
	 * @param keyword 검색어
	 * @param start 시작 레코드 번호
	 * @param rows 패치할 레코드 개수
	 * @return
	 */
	public static List<News> search(SQLiteDatabase db, String keyword, int start, int rows) {
		String sql = "SELECT * "
			       + "FROM   news "
			       + "WHERE  title LIKE '%"+keyword+"%' "
			       + "OR     body_text LIKE '%"+keyword+"%' "
			       + "ORDER BY uid DESC LIMIT "+rows+" OFFSET "+start;
		List<News> list = new ArrayList<News>();
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return list;
			}
			
			do {
				News entry = new News();
				entry.bind(c);
				list.add(entry);
			} while (c.moveToNext());
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return list;
	}
	
	public static News prevSearch(SQLiteDatabase db, int uid, String keyword) {
		String sql = "SELECT * "
			       + "FROM   news "
			       + "WHERE  uid > "+uid+" "
			       + "AND (  title LIKE '%"+keyword+"%' "
			       + "OR     body_text LIKE '%"+keyword+"%') "
			       + "ORDER  "
			       + "BY     uid ASC "
			       + "LIMIT  1";
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	public static News nextSearch(SQLiteDatabase db, int uid, String keyword) {
		String sql = "SELECT * "
		       + "FROM   news "
		       + "WHERE  uid < "+uid+" "
		       + "AND (  title LIKE '%"+keyword+"%' "
		       + "OR     body_text LIKE '%"+keyword+"%') "
		       + "ORDER  "
		       + "BY     uid DESC "
		       + "LIMIT  1";
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	/**
	 * 사진 글 목록
	 * @param start 시작 레코드 번호
	 * @param rows 패치할 레코드 개수
	 * @return
	 */
	public static List<News> listPhoto(SQLiteDatabase db, int start, int rows) {
		List<News> list = new ArrayList<News>();
		Cursor c = null;
		try {
			c = db.rawQuery("SELECT * FROM news WHERE photo_flag = 1 ORDER BY uid DESC LIMIT "+rows+" OFFSET "+start, null);
			if (!c.moveToFirst()) {
				return list;
			}
			
			do {
				News entry = new News();
				entry.bind(c);
				list.add(entry);
			} while (c.moveToNext());
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return list;
	}

	/**
	 * 사진 글 목록
	 * @param cate_uid 카테고리 번호
	 * @param start 시작 레코드 번호
	 * @param rows 패치할 레코드 개수
	 * @return
	 */
	public static List<News> listPhoto(SQLiteDatabase db, int cate_uid, int start, int rows) {
		List<News> list = new ArrayList<News>();
		Cursor c = null;
		try {
			c = db.rawQuery("SELECT * FROM news WHERE cate_uid = "+cate_uid+" AND photo_flag = 1 ORDER BY uid DESC LIMIT "+rows+" OFFSET "+start, null);
			if (!c.moveToFirst()) {
				return list;
			}
			
			do {
				News entry = new News();
				entry.bind(c);
				list.add(entry);
			} while (c.moveToNext());
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return list;
	}
	
	public static News prevPhoto(SQLiteDatabase db, int uid, int cid) {
		String sql = "SELECT * "
			       + "FROM   news "
			       + "WHERE  uid > "+uid+" "
			       + "AND    photo_flag = 1 ";
		
		if (cid > 0)
			sql += "AND cate_uid="+cid+" ";
		
		sql       += "ORDER  "
			       + "BY     uid ASC "
			       + "LIMIT  1";
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	public static News nextPhoto(SQLiteDatabase db, int uid, int cid) {
		String sql = "SELECT * "
		       + "FROM   news "
		       + "WHERE  uid < "+uid+" "
		       + "AND    photo_flag = 1 ";
		
		if (cid > 0)
			sql += "AND cate_uid="+cid+" ";
		
		sql   += "ORDER  "
		       + "BY     uid DESC "
		       + "LIMIT  1";
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	/**
	 * 동영상 글 목록
	 * @param start 시작 레코드 번호
	 * @param rows 패치할 레코드 개수
	 * @return
	 */
	public static List<News> listMovie(SQLiteDatabase db, int start, int rows) {
		List<News> list = new ArrayList<News>();
		Cursor c = null;
		try {
			c = db.rawQuery("SELECT * FROM news WHERE movie_flag = 1 ORDER BY uid DESC LIMIT "+rows+" OFFSET "+start, null);
			if (!c.moveToFirst()) {
				return list;
			}
			
			do {
				News entry = new News();
				entry.bind(c);
				list.add(entry);
			} while (c.moveToNext());
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return list;
	}

	/**
	 * 동영상 글 목록
	 * @param cate_uid 카테고리 번호
	 * @param start 시작 레코드 번호
	 * @param rows 패치할 레코드 개수
	 * @return
	 */
	public static List<News> listMovie(SQLiteDatabase db, int cate_uid, int start, int rows) {
		List<News> list = new ArrayList<News>();
		Cursor c = null;
		try {
			c = db.rawQuery("SELECT * FROM news WHERE cate_uid = "+cate_uid+" AND movie_flag = 1 ORDER BY uid DESC LIMIT "+rows+" OFFSET "+start, null);
			if (!c.moveToFirst()) {
				return list;
			}
			
			do {
				News entry = new News();
				entry.bind(c);
				list.add(entry);
			} while (c.moveToNext());
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return list;
	}
	
	public static News prevMovie(SQLiteDatabase db, int uid, int cid) {
		String sql = "SELECT * "
			       + "FROM   news "
			       + "WHERE  uid > "+uid+" "
			       + "AND    movie_flag = 1 ";
		
		if (cid > 0)
			sql += "AND cate_uid="+cid+" ";
		
		sql       += "ORDER  "
			       + "BY     uid ASC "
			       + "LIMIT  1";
		if (DebugFlag.isLogEnabled())
			Log.d(TAG, "prevMovie.sql="+sql);
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	public static News nextMovie(SQLiteDatabase db, int uid, int cid) {
		String sql = "SELECT * "
		       + "FROM   news "
		       + "WHERE  uid < "+uid+" "
		       + "AND    movie_flag = 1 ";
		
		if (cid > 0)
			sql += "AND cate_uid="+cid+" ";
		
		sql   += "ORDER  "
		       + "BY     uid DESC "
		       + "LIMIT  1";
		News entry = null;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return null;
			}
			entry = new News();
			entry.bind(c);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
		
		return entry;
	}
	
	public static int maximumId(SQLiteDatabase db) {
		String sql = "SELECT MAX(uid) FROM news";
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return 0;
			}
			
			return c.getInt(0);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
	}
	
	public static int maximumId(SQLiteDatabase db, int cate_uid) {
		String sql = "SELECT MAX(uid) FROM news WHERE cate_uid = " + cate_uid;
		Cursor c = null;
		try {
			c = db.rawQuery(sql, null);
			if (!c.moveToFirst()) {
				return 0;
			}
			
			return c.getInt(0);
		} finally {
			try { c.close(); } catch (Exception ignored) {}
		}
	}
}
